Skip to content

DEV-1247: BLE connect no longer blocks the main thread on the MTU; fail on notify/connect errors - #129

Merged
JongChern merged 7 commits into
masterfrom
DEV-1247_ble_connect_main_thread
Oct 20, 2026
Merged

JongChern merged 7 commits into
masterfrom
DEV-1247_ble_connect_main_thread

Conversation

@JongChern

@JongChern JongChern commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes two defects in Shimmer3BLEAndroid, the BLE path for Shimmer3/3R. The Android HIL (#128) found both.

Problem

  1. Every BLE connect blocked the UI thread for ~3.1 s. onConnectSuccess runs on FastBle's main-thread handler. It called setMtu(251) and then waited up to 3 s on mTaskMTU. FastBle 2.4.0 delivers onMtuChanged on that same main thread, so the wait always timed out. The MTU exchange itself finishes in about 25 ms. Logcat showed PerfMonitor longMsg ... wall≈3130ms h=com.clj.fastble.bluetooth.BleBluetooth$MainHandler on every connect, and MTU Changed was printed after <mac> Connected.
  2. Notify and connect failures were silent. onNotifyFailure and onConnectFail were empty. If the RX subscription failed, the driver still called initialize() and timed out on commands with nothing in the log. If the GATT connect failed, the state never changed.

Change

Only Shimmer3BLEAndroid.java changes.

  • onConnectSuccess sends the MTU request and returns. On the MTU result (success or failure), onMtuSet() requests connection priority, then the RX notification, then the PHY.
  • onNotifySuccess → onNotifyEnabled() does what the end of onConnectSuccess used to do: the CONNECTED message, the toast, the IO and processing threads, and initialize(). The order is MTU → notify → initialize, and nothing waits on the main thread.
  • A connect failure, a notify failure, or a missing RX characteristic or services list calls connectionFailed(reason). That logs the reason, disconnects, sets CONNECTION_FAILED and lets connect() return.
    • It sets CONNECTION_FAILED rather than DISCONNECTED because a first connect starts at DISCONNECTED, and the adapter only sends a state message when the state changes.
  • trySetResult on mTaskMTU and mTaskConnect guards against a callback arriving twice, e.g. FastBle's 5 s timeout followed by a late result.

Bench results

Android HIL, -Transport ble. Phone: Redmi M2101K6G, Android 11. Driver 0.11.9_beta. Shimmer3R on LogAndStream v1.1.21.

master this PR
connect, streamRest / midStreamSet 7.2–7.7 s 4.7 / 4.5 s
connect, reconnectLoop cycles 2–5 6.8–7.4 s 3.9–4.3 s
first CONNECTED message ~4.4 s ~1.4 s
PerfMonitor longMsg … BleBluetooth$MainHandler one per connect none, in any run
  • streamRest: PASS.
  • midStreamSet: fails with "2 timestamps did not increase". This is the known Shimmer3R firmware issue DEV-1245, which fails the same way on master; there were no other failures.
  • reconnectLoop: 3 of 5 runs passed, i.e. 20 of 22 connects. See the next section.

Not verified / open

  • Possible higher rate of GATT 133 connect failures. Open, needs a longer A/B soak. All of these were on the Shimmer3R, the same day.

    connects GATT 133 (HCI reason 0x3E) other
    master ~68 0 —
    this PR, incl. DEV-1250: Verisense BLE radio no longer blocks the main thread on the MTU; fail on notify/connect errors #130's radio ~110 5 one ~85 s stretch where the unit took no connection (4 attempts)
    • Each 133 came ~300 ms after Connecting, before FastBle reached onConnectSuccess, i.e. before any code this PR changes runs.
    • Fisher's test gives p ≈ 0.09, so this is suggestive, not proven.
    • I found no mechanism:
      • the disconnect-to-connect gap (~2.3 s), connection interval (12) and PHY (2M) are unchanged;
      • holding the link 4 s before disconnecting didn't prevent it (7 of 8), and an immediate disconnect went 12 of 12 in another run, so session length isn't the trigger.
    • What did change is timing: priority, PHY and the first commands now go out ~0.5 s after link-up instead of ~3.5 s.
    • Before this change, a 133 left the state at DISCONNECTED and the app waited out its timeout with nothing logged. Now it reports CONNECTION_FAILED within ~330 ms, with the GATT status in the log.
  • Shimmer3 not tested. The bench Shimmer3 was hung (DEV-1246, it stops answering commands). Its logcat still shows the new sequence on a Shimmer3: MTU Changed: 160 25 ms after configureMTU, then Notify Success, then Connected, with no longMsg.

  • Failure paths. Only the connect-failure path (the GATT 133s above) ran on hardware. The notify-failure and missing-RX paths did not.

Related

Jira: DEV-1247

🤖 Generated with Claude Code

… notify/connect errors

onConnectSuccess runs on FastBle's main-thread handler and waited 3 s for
an MTU result that FastBle delivers on that same thread, so the wait always
timed out and every connect froze the UI for ~3.1 s. Setup now continues
from the MTU callback (success or failure) and initialize() waits for the
RX notification to be enabled: MTU, then notify, then initialize.

A failed connect, a failed notify subscription or a missing RX
characteristic now logs the reason, disconnects, sets CONNECTION_FAILED
and lets connect() return, instead of leaving the driver timing out on
commands with nothing in the log.

HIL (-Transport ble, Shimmer3R): connect ~3 s faster, no PerfMonitor
longMsg from BleBluetooth$MainHandler.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

One or more issues must be addressed before approval.

4 open issues
What changed in this PR

Updates Shimmer3 BLE connection setup to avoid blocking on MTU negotiation and surface connection/notification failures.

Changes:

  • Continues setup asynchronously after MTU completion.
  • Delays initialization until RX notifications succeed.
  • Adds failure handling for connect, notify, and missing-characteristic cases.
File Description
ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java Updated as part of this pull request.

Review effort: Lite

…MTU task; no "lost" report for a failed connect

- connectionFailed() disconnects the BleDevice the failing callback was
  given, not mBleDevice, which is unset or stale on the onConnectFail path.
- The MTU callback completes a task local to its attempt, so a late MTU
  result after a timeout can't complete a later attempt's task.
- onDisConnected only reports DISCONNECTED and "connection was lost" for a
  connection that reached Connected. A failed setup is already reported as
  CONNECTION_FAILED, and the toasts contradicted each other.

Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:20
JongChern added a commit that referenced this pull request Oct 20, 2026
…r-attempt MTU task

The same two fixes as DEV-1247's review, for the identical code here:
- connectionFailed() disconnects the BleDevice the failing callback was
  given, not mBleDevice, which is unset or stale on the onConnectFail path.
- The MTU callback completes a task local to its attempt, so a late MTU
  result after a timeout can't complete a later attempt's task.

Probe through Shimmer3RAndroidRadioByteCommunication on a Shimmer3R: 5/5
connects at ~1.5 s, first command answered every time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

One or more issues must be addressed before approval.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Distinguish missing GATT services from missing RX characteristic

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:271

When getBluetoothGattServices() returns null, this reports “RX characteristic not found,” which is a different failure and makes the new connect-failure diagnostic misleading. Report the missing services list separately, and reserve the RX message for a non-null list that lacks the characteristic.

3 resolved since last review

Review effort: Lite

… late success after a failure

- onDisConnected before setup finished now calls connectionFailed(), so
  connect() returns and CONNECTION_FAILED is set straight away instead of
  after an MTU/notify timeout or connect()'s 10 s wait.
- connect() keeps this attempt's own task. onDisConnected, onConnectSuccess
  and the wait use it, so a late callback from an earlier attempt can't
  complete or fail a newer one.
- A late onConnectSuccess for an attempt that has already failed is
  disconnected instead of set up.
- A missing services list is reported as such, not as a missing RX
  characteristic.
- Comments on the per-attempt tasks spelled out.

Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

One or more issues must be addressed before approval.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Late notify callback can initialize a stale GATT attempt

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:220

This completes the mutable mTaskConnect, which may already belong to a later attempt. If an attempt disconnects after onConnectSuccess but before notification completes, its late MTU/notify callback can reach here after a reconnect starts and mark the new attempt Connected, start IO against the old GATT, and initialize it. Carry the captured attempt task/generation through onMtuSet, startServiceS, and the notify callbacks, and ignore callbacks that are no longer current.

2 resolved since last review

Review effort: Lite

…etup step

onMtuSet, startServiceS, newConnectedBLEDevice and their callbacks now carry
the task of the connect attempt they belong to. onNotifyEnabled and
connectionFailed ignore an attempt that is no longer current, so a late
notify or service result from an earlier attempt can't mark a newer one
Connected, fail it, or disconnect it. The public startServiceS and
newConnectedBLEDevice keep their signatures and use the current attempt.

Shimmer3R BLE HIL: streamRest and reconnectLoop (5/5) pass, no main-thread
longMsg.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:44
onConnectSuccess returns at once if a newer connect() has replaced this
attempt's task, so it can't overwrite the newer attempt's device, buffer or
MTU task. It doesn't disconnect: the newer attempt owns the same device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Reject stale connection success before updating shared state

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:136

The late-success guard checks only whether this attempt is completed, not whether it is still the current attempt. If a newer connect() has replaced mTaskConnect before this callback arrives, this stale success still overwrites the shared mBuffer, mBleDevice, and mTaskMTU; the newer connection can then use the wrong buffer/device. Reject a success whose task is no longer mTaskConnect before changing shared state.

Medium severity Ignore active or stale disconnect callbacks as connection failures

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:169

This treats every pre-Connected disconnect as a connection failure, including an active disconnect requested by the caller (isActiveDisConnected == true), and it does not ignore a disconnect callback from an older attempt. That can overwrite an intentional DISCONNECTED state with CONNECTION_FAILED, or emit failure for a later reconnect. Return for active or non-current callbacks before applying the setup-failure path.

Medium severity Guard late MTU callbacks after connection attempt failure

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:205

A late MTU callback can arrive after onDisConnected or another failure has already completed this attempt. onMtuSet() does not re-check the attempt before calling requestConnectionPriority() and starting service/notify setup, so it can operate on a disconnected/stale GATT and register a notification after failure. Guard this continuation with the same current-task/completed check used by onNotifyEnabled() and disconnect/return when it is stale.

1 resolved since last review

Review effort: Lite

Copilot AI lite review requested due to automatic review settings October 20, 2026 07:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Guard MTU setup against obsolete connection attempts

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:212

The MTU callback is attempt-local, but this method does not check whether taskConnect is still the current, incomplete attempt. After a reconnect or an earlier failure, a late MTU result can still request priority/PHY and enable notifications on the stale GATT; only the later onNotifyEnabled call is ignored. Guard this method before starting service setup so an obsolete attempt cannot continue setup.

Review effort: Lite

…lure; skip a late MTU result

- onDisConnected before setup finished: an active disconnect (the caller's
  own disconnect()) ends the attempt quietly instead of setting
  CONNECTION_FAILED over the DISCONNECTED it already set. Only a dropped
  link is reported as a failed connect.
- onMtuSet returns at once if its attempt has failed or been replaced while
  the MTU was pending, so it doesn't request priority or notifications on a
  dead or stale link.

Shimmer3R BLE HIL reconnectLoop 5/5, no main-thread longMsg.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 07:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Ignore stale disconnect callbacks from previous connection attempts

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:173

This callback is not filtered by attempt identity once the old task has reached Connected. If disconnect() is followed quickly by a new connect() on the same instance, the old GATT's delayed onDisConnected enters this branch and emits DISCONNECTED/"Device connection was lost" while the newer attempt is active. Return immediately when taskConnect != mTaskConnect, before inspecting the task result.

Review effort: Lite

onDisConnected returns at once if a newer connect() has replaced this
attempt, so an old link going down after disconnect() and a quick reconnect
can't report DISCONNECTED or "connection was lost" over the new attempt.

Shimmer3R BLE HIL reconnectLoop: cycles 1-4 connect and report their
disconnects as before. Cycle 5 hit the separate GATT 133 at link setup that
is open on the PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 20, 2026 08:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

One or more issues must be addressed before approval.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Stale characteristic updates can contaminate the current connection

ShimmerAndroidInstrumentDriver/​ShimmerAndroidInstrumentDriver/​src/​main/​java/​com/​shimmerresearch/​androidradiodriver/​Shimmer3BLEAndroid.java:331

The attempt token now guards notify success/failure, but this same notification callback still writes every characteristic update into the shared mBuffer. If a delayed callback from a failed or older attempt arrives after a reconnect, those stale bytes can be consumed as responses for the current connection. Ignore updates unless this task is still current and either setup is pending or it completed as Connected.

Review effort: Lite

@JongChern
JongChern merged commit c19f1ec into master Oct 20, 2026
2 checks passed
@JongChern
JongChern deleted the DEV-1247_ble_connect_main_thread branch October 20, 2026 08:16
@JongChern

Copy link
Copy Markdown
Contributor Author

Soak: GATT 133 rate before vs after this change

This settles the open question in the description. The setup:

Arm Connect attempts Connected GATT 133 (HCI 0x3E) No link within FastBle's 10 s
before 111 100 4 7
after 110 105 2 3

No regression. GATT 133s and no-link attempts happen on the old code too, in clusters that hit whichever arm happened to be running. One-sided Fisher p for "after is worse": 0.89 for 133s, 0.97 for any connect failure. The difference seen earlier (5/110 vs 0/68) came from comparing different sessions.

One unexplained episode, recorded but not attributed: in round 4's "after" block, for about 3.5 minutes, the unit accepted connections but didn't answer a single command.

  • The link side was fine: link setup completed and notifications were enabled every time.
  • It recovered by itself.
  • It matches the symptom described for a Shimmer3 in DEV-1246.
  • Nothing this PR changes runs after the link is up, so it isn't evidence either way.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants